package com.loongtech.bi.action.ios;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.loongtech.bi.entity.ios.EntityMonthProductManager;
import com.loongtech.bi.manager.ios.MonthProductManager;
import com.loongtech.core.util.Time;
import com.loongtech.core.util.Utils;

@Controller("monthProductManagerAction")
@Scope("view")
public class MonthProductManagerAction {

	private Date lookMonth = new Date();

	@Autowired
	private MonthProductManager manager;

	private List<EntityMonthProductManager> list;

	private EntityMonthProductManager entity;

	private Map<String, String> productItmes; // 产品下拉列表

	private Map<String, String> projectItmes; // 工程下拉列表

	public Map<String, String> getProductItmes() {
		return productItmes;
	}

	public void setProductItmes(Map<String, String> productItmes) {
		this.productItmes = productItmes;
	}

	public Map<String, String> getProjectItmes() {
		return projectItmes;
	}

	public void setProjectItmes(Map<String, String> projectItmes) {
		this.projectItmes = projectItmes;
	}

	@Autowired
	public void init() {
		initData();
	}

	private void initData() {
		productItmes = manager.getProductMap();
		projectItmes = manager.getProjectMap();
		entity = new EntityMonthProductManager();
		search();
	}

	public void search() {
		List<Object> cons = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer(" from EntityMonthProductManager l where 1=1 ");

		String date = lookMonth == null ? null : Time.formatDate(lookMonth, "yyyy-MM");

		if (!Utils.isEmptyStr(date)) {
			hql.append(" and l.lookMonth = ? ");
			cons.add(date);
		}
		if (!Utils.isEmptyStr(entity.getProductid())) {
			hql.append(" and l.productid = ? ");
			cons.add(entity.getProductid());
		}
		if (!Utils.isEmptyStr(entity.getCost())) {
			hql.append(" and l.cost = ? ");
			cons.add(entity.getCost());
		}
		if (!Utils.isEmptyStr(entity.getUnsubscribe())) {
			hql.append(" and l.unsubscribe = ? ");
			cons.add(entity.getUnsubscribe());
		}
		if (!Utils.isEmptyStr(entity.getProjectid())) {
			hql.append(" and l.projectid = ? ");
			cons.add(entity.getProjectid());
		}
		list = manager.findByQuery(hql.toString(), cons.toArray());
	}

	public void add() {
		FacesContext context = FacesContext.getCurrentInstance();
		try {
			entity.setLookMonth(Time.formatDate(lookMonth, "yyyy-MM"));
			if (validate(context, entity)) {
				manager.persist(entity);
				context.addMessage(null, new FacesMessage("添加成功！"));
				initData();
			}
		} catch (Exception e) {
			context.addMessage(null, new FacesMessage("添加失败！"));
		}
	}

	public void modify(EntityMonthProductManager e) {
		FacesContext context = FacesContext.getCurrentInstance();
		try {
			if (validate(context, e)) {
				manager.merge(e);
				context.addMessage(null, new FacesMessage("修改成功！"));
				initData();
			}
		} catch (Exception ex) {
			context.addMessage(null, new FacesMessage("修改失败！" ));
		}
	}

	public void delete(int id) {
		FacesContext context = FacesContext.getCurrentInstance();
		try {
			if (!Utils.isEmptyStr(String.valueOf(id))) {
				manager.removeById(id);
			}
			context.addMessage(null, new FacesMessage("删除成功！"));
			initData();
		} catch (Exception e) {
			context.addMessage(null, new FacesMessage("删除失败！"));
		}
	}

	private boolean validate(FacesContext context, EntityMonthProductManager ebp) {
		if (Utils.isEmptyStr(ebp.getLookMonth())) {
			context.addMessage(null, new FacesMessage("日期不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(ebp.getCost())) {
			context.addMessage(null, new FacesMessage("销售数不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(ebp.getUnsubscribe())) {
			context.addMessage(null, new FacesMessage("退定数不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(ebp.getProductid())) {
			context.addMessage(null, new FacesMessage("产品id选择不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(ebp.getProjectid())) {
			context.addMessage(null, new FacesMessage("工程id选择不能为空！"));
			return false;
		}
		return true;
	}

	public List<EntityMonthProductManager> getList() {
		return list;
	}

	public void setList(List<EntityMonthProductManager> list) {
		this.list = list;
	}

	public EntityMonthProductManager getEntity() {
		return entity;
	}

	public void setEntity(EntityMonthProductManager entity) {
		this.entity = entity;
	}

	public static void main(String[] args) {
	}

	public Date getLookMonth() {
		return lookMonth;
	}

	public void setLookMonth(Date lookMonth) {
		this.lookMonth = lookMonth;
	}

}
